/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.gal.GalSearchConfig.GalType;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.json.JSONException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalContact
/*     */   implements Comparable
/*     */ {
/*     */   private GalSearchConfig.GalType mGalType;
/*     */   private Map<String, Object> mAttrs;
/*     */   private String mId;
/*     */   private String mSortField;
/*     */   
/*     */   public GalContact(String dn, Map<String, Object> attrs)
/*     */   {
/*  51 */     this.mId = dn;
/*  52 */     this.mAttrs = attrs;
/*     */   }
/*     */   
/*     */   public GalContact(GalSearchConfig.GalType galType, String dn, Map<String, Object> attrs) {
/*  56 */     this.mGalType = galType;
/*  57 */     this.mId = dn;
/*  58 */     this.mAttrs = attrs;
/*     */   }
/*     */   
/*     */   public boolean isZimbraGal() {
/*  62 */     return GalSearchConfig.GalType.zimbra == this.mGalType;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getId()
/*     */   {
/*  69 */     return this.mId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public Map<String, Object> getAttrs()
/*     */   {
/*  76 */     return this.mAttrs;
/*     */   }
/*     */   
/*     */   public String toString() {
/*  80 */     StringBuffer sb = new StringBuffer();
/*  81 */     sb.append("LdapGalContact: { ");
/*  82 */     sb.append("id=" + this.mId);
/*  83 */     sb.append("}");
/*  84 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public String getSingleAttr(String name) {
/*  88 */     Object val = this.mAttrs.get(name);
/*  89 */     String strValue = null;
/*  90 */     if ((val instanceof String)) {
/*  91 */       strValue = (String)val;
/*     */       try {
/*  93 */         String[] vals = Contact.parseMultiValueAttr(strValue);
/*  94 */         if (vals.length >= 1) {
/*  95 */           strValue = vals[0];
/*     */         }
/*     */       } catch (JSONException jex) {
/*  98 */         ZimbraLog.mailop.debug("Not a valid JSON format : %s", strValue, jex);
/*     */       }
/* 100 */     } else if ((val instanceof String[])) {
/* 101 */       strValue = ((String[])(String[])val)[0];
/*     */     }
/* 103 */     return strValue;
/*     */   }
/*     */   
/*     */   public boolean isGroup() {
/* 107 */     return "group".equals(getSingleAttr(ContactConstants.A_type));
/*     */   }
/*     */   
/*     */   private String getSortField() {
/* 111 */     if (this.mSortField != null) { return this.mSortField;
/*     */     }
/* 113 */     this.mSortField = getSingleAttr(ContactConstants.A_fullName);
/* 114 */     if (this.mSortField != null) { return this.mSortField;
/*     */     }
/* 116 */     String first = getSingleAttr(ContactConstants.A_firstName);
/* 117 */     String last = getSingleAttr(ContactConstants.A_lastName);
/*     */     
/* 119 */     if ((first != null) || (last != null)) {
/* 120 */       StringBuilder sb = new StringBuilder();
/* 121 */       if (first != null) {
/* 122 */         sb.append(first);
/*     */       }
/* 124 */       if (last != null) {
/* 125 */         if (sb.length() > 0) sb.append(' ');
/* 126 */         sb.append(last);
/*     */       }
/* 128 */       this.mSortField = sb.toString();
/*     */     } else {
/* 130 */       this.mSortField = getSingleAttr(ContactConstants.A_email);
/* 131 */       if (this.mSortField == null) this.mSortField = "";
/*     */     }
/* 133 */     return this.mSortField;
/*     */   }
/*     */   
/*     */   public int compareTo(Object obj) {
/* 137 */     if (!(obj instanceof GalContact))
/* 138 */       return 0;
/* 139 */     GalContact other = (GalContact)obj;
/* 140 */     return getSortField().compareTo(other.getSortField());
/*     */   }
/*     */   
/*     */   public static GalContact fromElement(Element elm) throws ServiceException {
/* 144 */     String dn = elm.getAttribute("ref");
/* 145 */     Map<String, Object> attrs = new HashMap();
/* 146 */     for (Element attr : elm.listElements("a")) {
/* 147 */       String name = attr.getAttribute("n");
/* 148 */       String value = attr.getText();
/* 149 */       StringUtil.addToMultiMap(attrs, name, value);
/*     */     }
/* 151 */     GalContact galContact = new GalContact(dn, attrs);
/* 152 */     return galContact;
/*     */   }
/*     */   
/*     */   public static abstract interface Visitor
/*     */   {
/*     */     public abstract void visit(GalContact paramGalContact)
/*     */       throws ServiceException;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/GalContact.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */